#include <iostream>  
#include <semaphore.h>  
#include <unistd.h>  
#include <fcntl.h>  
#include <sys/stat.h>  
  
int main() {  
    sem_t *sem = sem_open("/mysemaphore", 0); // 打开已存在的信号量  
    if (sem == SEM_FAILED) {  
        perror("sem_open");  
        return 1;  
    }  
  
    std::cout << "Process 1: Waiting for semaphore..." << std::endl;  
    sem_wait(sem); // 等待信号量  
    std::cout << "Process 1: Accessing resource..." << std::endl;  
    sleep(2); // 模拟访问资源  
    std::cout << "Process 1: Resource access complete, releasing semaphore..." << std::endl;  
    sem_post(sem); // 释放信号量  
  
    sem_close(sem); // 关闭信号量描述符（但信号量本身不会删除）  
    return 0;  
}